

* Converting to spmatrix format : based on spatial_3_*


	** What this file does: 
		 
	** a) PLACEHOLDER, can be run for different matrices to save spatial objects
	** b) ACTIVATOR, calls up existing matrices (needed for creation of spatial lag)
	** c) CONNECTOR, puts the product space together with the spatial variable 
	
	** Contents: robustness check on matrix overlaps
	
	** 1) Extract vertical matrix from product space 
	** 2) Merge with other matrices to identify overlap 
	** 3) Create disjoint alternatives for all specifications 
	** 4) Transform to spatial lag variable in main regression data 

capture log close 
clear all
set more off 
set maxvar 30000, permanently // required to load matrix 

* Set directory
global server "R:\WSV2\TBu_AKe\Spatial_NEW" 
cd "$server" // set working directory 

capture mkdir Wmat_SCB_overlap_0102
global store "${server}\Wmat_SCB_overlap_0102"

*Define sub directories
global desktop "C:\Users\hy65byfe\Desktop\smerge_0712"
*cd "$desktop" // set working directory 


log using spatial_4_spgen_overlap_0102, replace 


cd "$store"
cd // display in log file 	
	

	*************************************
	** 1) W matrix, save vertical only **
	*************************************

use "R:\WSV2\TBu_AKe\Spatial_NEW\Wmat_Fan\spatial_3_wmat_fan_5678.dta"


keep *cell a_bin a_mid e_* s* // reduce 

rename s6_* v_* // rename the vertical matrix 

preserve 

keep cell a_bin v* 

save wmat_vert_fan, replace 

restore	

clear 

use "R:\WSV2\TBu_AKe\Spatial_NEW\Wmat_4corners_reduced\spatial_3_wmat_4corners_5678.dta"


keep *cell a_bin a_mid e_* s* // reduce 

rename s6_* v_* // rename the vertical matrix 

preserve 

keep cell a_bin v* 

save wmat_vert_4corners, replace 

restore	

clear 


use "R:\WSV2\TBu_AKe\Spatial_NEW\Wmat_Beam\spatial_3_wmat_beam_5678.dta"


keep *cell a_bin a_mid e_* s* // reduce 

rename s6_* v_* // rename the vertical matrix 

preserve 

keep cell a_bin v* 

save wmat_vert_beam, replace 

restore	

clear 

		********************************************************************************
		*** 2) + 3) load each of the 6 data sets: merge with vertical, define overlap **
		********************************************************************************

		
		*** Fan, 5-8 ***
use "R:\WSV2\TBu_AKe\Spatial_NEW\Wmat_Fan\spatial_3_wmat_fan_5678.dta" 
drop m* 

merge 1:1 cell using wmat_vert_fan 


forvalues i = 1/8 {
	
	forvalues j = 1/4950 {
		
		capture quietly replace s`i'_`j' = 0 if v_`j' == 1
	
	}
		
}
 	

xtset cell 
spset cell // must by spset. 

cd "$store"


		*** S5
spmatrix fromdata S5 = s5_1 - s5_4950, normalize(none) 

spmatrix save S5 using S5.stswm, replace // inverse: spmatrix use 


		*** S6
spmatrix fromdata S6 = v_1 - v_4950, normalize(none) // *!* 

spmatrix save S6 using S6.stswm, replace // inverse: spmatrix use 

 
		*** S7
spmatrix fromdata S7 = s7_1 - s7_4950, normalize(none) 

spmatrix save S7 using S7.stswm, replace // inverse: spmatrix use 


		*** S8
spmatrix fromdata S8 = s8_1 - s8_4950, normalize(none) 

spmatrix save S8 using S8.stswm, replace // inverse: spmatrix use 

clear


		*** Fan, 1-4 ***
use "R:\WSV2\TBu_AKe\Spatial_NEW\Wmat_Fan\spatial_3_wmat_fan_1234.dta" 
drop m* 

merge 1:1 cell using wmat_vert_fan 


forvalues i = 1/8 {
	
	forvalues j = 1/4950 {
		
		capture quietly replace s`i'_`j' = 0 if v_`j' == 1
	
	}
		
}
 	

xtset cell 
spset cell // must by spset. 

cd "$store"

		*** S1
spmatrix fromdata S1 = s1_1 - s1_4950, normalize(none) 

spmatrix save S1 using S1.stswm, replace // inverse: spmatrix use 

		*** S2
spmatrix fromdata S2 = s2_1 - s2_4950, normalize(none) 

spmatrix save S2 using S2.stswm, replace // inverse: spmatrix use 

 
		*** S3
spmatrix fromdata S3 = s3_1 - s3_4950, normalize(none) 

spmatrix save S3 using S3.stswm, replace // inverse: spmatrix use 


		*** S4
spmatrix fromdata S4 = s4_1 - s4_4950, normalize(none) 

spmatrix save S4 using S4.stswm, replace // inverse: spmatrix use 


clear

	
		*** Corners, 5-8 ***
use "R:\WSV2\TBu_AKe\Spatial_NEW\Wmat_4corners_reduced\spatial_3_wmat_4corners_5678.dta" 
capture drop m* 

merge 1:1 cell using wmat_vert_4corners


forvalues i = 1/8 {
	
	forvalues j = 1/4950 {
		
		capture quietly replace s`i'_`j' = 0 if v_`j' == 1
	
	}
		
}
 	

xtset cell 
spset cell // must by spset. 

cd "$store"


		*** C5
spmatrix fromdata C5 = s5_1 - s5_4950, normalize(none) 

spmatrix save C5 using C5.stswm, replace // inverse: spmatrix use 


		*** C6
spmatrix fromdata C6 = v_1 - v_4950, normalize(none) 

spmatrix save C6 using C6.stswm, replace // inverse: spmatrix use 

 
		*** C7
spmatrix fromdata C7 = s7_1 - s7_4950, normalize(none) 

spmatrix save C7 using C7.stswm, replace // inverse: spmatrix use 


		*** C8
spmatrix fromdata C8 = s8_1 - s8_4950, normalize(none) 

spmatrix save C8 using C8.stswm, replace // inverse: spmatrix use 

clear 


		*** Corners, 1-4 ***
use "R:\WSV2\TBu_AKe\Spatial_NEW\Wmat_4corners_reduced\spatial_3_wmat_4corners_1234.dta" 
capture drop m* 

merge 1:1 cell using wmat_vert_4corners


forvalues i = 1/8 {
	
	forvalues j = 1/4950 {
		
		capture quietly replace s`i'_`j' = 0 if v_`j' == 1
	
	}
		
}
 	

xtset cell 
spset cell // must by spset. 

cd "$store"

		*** C1
spmatrix fromdata C1 = s1_1 - s1_4950, normalize(none) 

spmatrix save C1 using C1.stswm, replace // inverse: spmatrix use 

		*** C2
spmatrix fromdata C2 = s2_1 - s2_4950, normalize(none) 

spmatrix save C2 using C2.stswm, replace // inverse: spmatrix use 

 
		*** C3
spmatrix fromdata C3 = s3_1 - s3_4950, normalize(none) 

spmatrix save C3 using C3.stswm, replace // inverse: spmatrix use 


		*** C4
spmatrix fromdata C4 = s4_1 - s4_4950, normalize(none) 

spmatrix save C4 using C4.stswm, replace // inverse: spmatrix use 


clear 
	
	
	
		*** Beam, 5-8 ***
use "R:\WSV2\TBu_AKe\Spatial_NEW\Wmat_Beam\spatial_3_wmat_beam_5678.dta" 
capture drop m* 

merge 1:1 cell using wmat_vert_beam


forvalues i = 1/8 {
	
	forvalues j = 1/4950 {
		
		capture quietly replace s`i'_`j' = 0 if v_`j' == 1 
	
	}
		
}
 	

xtset cell 
spset cell // must by spset. 

cd "$store"


		*** B5
spmatrix fromdata B5 = s5_1 - s5_4950, normalize(none) 

spmatrix save B5 using B5.stswm, replace // inverse: spmatrix use 


		*** B6
spmatrix fromdata B6 = v_1 - v_4950, normalize(none) // *!* 

spmatrix save B6 using B6.stswm, replace // inverse: spmatrix use 

 
		*** B7
spmatrix fromdata B7 = s7_1 - s7_4950, normalize(none) 

spmatrix save B7 using B7.stswm, replace // inverse: spmatrix use 


		*** B8
spmatrix fromdata B8 = s8_1 - s8_4950, normalize(none) 

spmatrix save B8 using B8.stswm, replace // inverse: spmatrix use 

clear 


		*** Beam, 1-4 ***
use "R:\WSV2\TBu_AKe\Spatial_NEW\Wmat_Beam\spatial_3_wmat_beam_1234.dta" 
capture drop m* 

merge 1:1 cell using wmat_vert_beam


forvalues i = 1/8 {
	
	forvalues j = 1/4950 {
		
		capture quietly replace s`i'_`j' = 0 if v_`j' == 1
	
	}
		
}
 	

xtset cell 
spset cell // must by spset. 

cd "$store"

		*** B1
spmatrix fromdata B1 = s1_1 - s1_4950, normalize(none) 

spmatrix save B1 using B1.stswm, replace // inverse: spmatrix use 

		*** B2
spmatrix fromdata B2 = s2_1 - s2_4950, normalize(none) 

spmatrix save B2 using B2.stswm, replace // inverse: spmatrix use 

 
		*** B3
spmatrix fromdata B3 = s3_1 - s3_4950, normalize(none) 

spmatrix save B3 using B3.stswm, replace // inverse: spmatrix use 


		*** B4
spmatrix fromdata B4 = s4_1 - s4_4950, normalize(none) 

spmatrix save B4 using B4.stswm, replace // inverse: spmatrix use 


clear 
/* 	
			***** load all matrices *** 
			
forvalues i = 1/8 {
	
	capture spmatrix use S`i' using S`i'.stswm, replace	
	capture spmatrix use C`i' using C`i'.stswm, replace	
	capture spmatrix use B`i' using B`i'.stswm, replace	
	
	}
			
*/ 
	*************************************************
	* 4)  Transform weight matrix to Finished Data **
	*************************************************


*cd "R:\WSV2\TBu_AKe\Spatial_NEW"
cd "C:\Users\hy65byfe\Desktop\smerge_0712"
use spatial_6_regs_s_monthly, replace

drop if missing(date) // 485,100 obs initially, already cleared here. 
 
*** spset 
xtset cell kt // kt: cntry-year observation: must be balanced. 
spset cell  // set identifier
 

*******************************************************************
	 ** basic reg: index: i = product, k = cell, var kt = country-year pairs
		* HERE: n
		* log_x_k,t = wx_k,t-1 + log_x_k,t-1 + e_i,t(clustered)
********************************************************************
 
			***************************
			** Fill in the blanks *****
			***************************

******** Outcome: sales 
		** lag, 
*codebook L3_sd // 9k missing
replace L3_sd = 0 if missing(L3_sd) // assume: missing is a zero.
replace L3_lsd = 0 if missing(L3_lsd)  // assume: missing as true missing or missing as zero are actually 1 single units. 

		** contemp. 
*codebook log_sd
replace log_sd = 0 if missing(log_sd) // 286k 

******** Outcome: count 
		** lag
		
*codebook L3_cd // 9k missing
replace L3_cd = 0 if missing(L3_cd) // assume: missing is a zero. probably reasonable. 
replace L3_lcd = 0 if missing(L3_lcd)  // assume: missing as true missing or missing as zero are actually 1 single units. not reasonable. 
		
		** contemp. 
*codebook log_cd
replace log_cd = 0 if missing(log_cd)


******** Load Matrices
			
spmatrix dir // check active matrices. MANUAL: if cleared all must activate with spmatrix use. 

cd "$store"

forvalues i = 1/8 {
	capture spmatrix use S`i' using S`i'.stswm, replace 
	capture spmatrix use C`i' using C`i'.stswm, replace 
	capture spmatrix use B`i' using B`i'.stswm, replace 
}

spmatrix dir

******** Gen Lags for outcome variables


 forvalues i = 1/8 {
	gen S`i'_L3_sd = 0 
	gen S`i'_L3_cd = 0 
	
	gen C`i'_L3_sd = 0 
	gen C`i'_L3_cd = 0 
	
	gen B`i'_L3_sd = 0 
	gen B`i'_L3_cd = 0 
}
 

keep if year == 2014 // recall: 'time' is country in the spstructure. 

		* loop logic is following: go through 4 outcome vars. 
			* for each one, go country by country, create auxiliary spgen
			* then combine all into 1 var. drop auxiliaries 
			

egen k14 = group(ccode date) // with monthly data, repeated values in year. new identifier keeps spgen running. 	
/* 
browse if missing(k14)
tab month if missing(k14)
*/ 

drop if missing(date) // 34,650, all cleared here already 
	
sum k14, meanonly  // date ccode group. 
scalar kmin = `r(min)' // store in scalars if sub-sample used later. 
scalar kmax = `r(max)'
	
scalar list kmin kmax // 1 to 84 
 
 *** spset 
xtset cell k14 // kt: cntry-year observation: must be balanced.
capture spset cell  // set identifier

** CHECKPOINT: if this works, the data is set up correctly.

 ***  S1 - 8 
 
foreach var of varlist L3_sd L3_cd {
 	forvalues i = `=kmin'/`=kmax' {
	local k = `i' 
		
      spgenerate Waux_`var'_`i' = S1*`var' if k14==`k'
         quietly replace S1_`var'=Waux_`var'_`i' if k14==`k'
		 drop Waux* 
		 
	   spgenerate Waux_`var'_`i' = C1*`var' if k14==`k'
         quietly replace C1_`var'=Waux_`var'_`i' if k14==`k'
		 drop Waux* 
		 
		spgenerate Waux_`var'_`i' = B1*`var' if k14==`k'
         quietly replace B1_`var'=Waux_`var'_`i' if k14==`k'
		 drop Waux* 
         } 
 } 
 
 
 foreach var of varlist L3_sd L3_cd {
 	forvalues i = `=kmin'/`=kmax' {
	local k = `i' 
		
      spgenerate Waux_`var'_`i' = S2*`var' if k14==`k'
         quietly replace S2_`var'=Waux_`var'_`i' if k14==`k'
		 drop Waux* 
		 
	   spgenerate Waux_`var'_`i' = C2*`var' if k14==`k'
         quietly replace C2_`var'=Waux_`var'_`i' if k14==`k'
		 drop Waux* 
		 
		spgenerate Waux_`var'_`i' = B2*`var' if k14==`k'
         quietly replace B2_`var'=Waux_`var'_`i' if k14==`k'
		 drop Waux* 
         } 
 } 
 
 
  foreach var of varlist L3_sd L3_cd {
 	forvalues i = `=kmin'/`=kmax' {
	local k = `i' 
      spgenerate Waux_`var'_`i' = S3*`var' if k14==`k'
         quietly replace S3_`var'=Waux_`var'_`i' if k14==`k'
		 drop Waux* 
		 
	   spgenerate Waux_`var'_`i' = C3*`var' if k14==`k'
         quietly replace C3_`var'=Waux_`var'_`i' if k14==`k'
		 drop Waux* 
		 
		spgenerate Waux_`var'_`i' = B3*`var' if k14==`k'
         quietly replace B3_`var'=Waux_`var'_`i' if k14==`k'
		 drop Waux* 
         } 
 } 
 
 
 
  foreach var of varlist L3_sd L3_cd {
 	forvalues i = `=kmin'/`=kmax' {
	local k = `i' 
		
      spgenerate Waux_`var'_`i' = S4*`var' if k14==`k'
         quietly replace S4_`var'=Waux_`var'_`i' if k14==`k'
		 drop Waux* 
		 
	   spgenerate Waux_`var'_`i' = C4*`var' if k14==`k'
         quietly replace C4_`var'=Waux_`var'_`i' if k14==`k'
		 drop Waux* 
		 
		spgenerate Waux_`var'_`i' = B4*`var' if k14==`k'
         quietly replace B4_`var'=Waux_`var'_`i' if k14==`k'
		 drop Waux* 
         } 
 } 
 
 
 foreach var of varlist L3_sd L3_cd {
 	forvalues i = `=kmin'/`=kmax' {
	local k = `i' 
		
      spgenerate Waux_`var'_`i' = S5*`var' if k14==`k'
         quietly replace S5_`var'=Waux_`var'_`i' if k14==`k'
		 drop Waux* 
		 
	   spgenerate Waux_`var'_`i' = C5*`var' if k14==`k'
         quietly replace C5_`var'=Waux_`var'_`i' if k14==`k'
		 drop Waux* 
		 
		spgenerate Waux_`var'_`i' = B5*`var' if k14==`k'
         quietly replace B5_`var'=Waux_`var'_`i' if k14==`k'
		 drop Waux* 
         } 
 } 
 
 
 foreach var of varlist L3_sd L3_cd {
 	forvalues i = `=kmin'/`=kmax' {
	local k = `i' 
		
      spgenerate Waux_`var'_`i' = S6*`var' if k14==`k'
         quietly replace S6_`var'=Waux_`var'_`i' if k14==`k'
		 drop Waux* 
		 
	   spgenerate Waux_`var'_`i' = C6*`var' if k14==`k'
         quietly replace C6_`var'=Waux_`var'_`i' if k14==`k'
		 drop Waux* 
		 
		spgenerate Waux_`var'_`i' = B6*`var' if k14==`k'
         quietly replace B6_`var'=Waux_`var'_`i' if k14==`k'
		 drop Waux* 
         } 
 } 

 
 
foreach var of varlist L3_sd L3_cd {
 	forvalues i = `=kmin'/`=kmax' {
	local k = `i' 
		
      spgenerate Waux_`var'_`i' = S7*`var' if k14==`k'
         quietly replace S7_`var'=Waux_`var'_`i' if k14==`k'
		 drop Waux* 
		 
	   spgenerate Waux_`var'_`i' = C7*`var' if k14==`k'
         quietly replace C7_`var'=Waux_`var'_`i' if k14==`k'
		 drop Waux* 
		 
		spgenerate Waux_`var'_`i' = B7*`var' if k14==`k'
         quietly replace B7_`var'=Waux_`var'_`i' if k14==`k'
		 drop Waux* 
         } 
 }  
 
 
 
foreach var of varlist L3_sd L3_cd {
 	forvalues i = `=kmin'/`=kmax' {
	local k = `i' 
		
      spgenerate Waux_`var'_`i' = S8*`var' if k14==`k'
         quietly replace S8_`var'=Waux_`var'_`i' if k14==`k'
		 drop Waux* 
		 
	   spgenerate Waux_`var'_`i' = C8*`var' if k14==`k'
         quietly replace C8_`var'=Waux_`var'_`i' if k14==`k'
		 drop Waux* 
		 
		spgenerate Waux_`var'_`i' = B8*`var' if k14==`k'
         quietly replace B8_`var'=Waux_`var'_`i' if k14==`k'
		 drop Waux* 
         } 
 }  
 
 
browse cell year ccode S* C* B* L3_sd L3_cd if cell <= 50 & year == 2014 // looks ok? 

save spatial_regs_s_monthly_0102_overlap, replace

clear

